package com.tuishan.myapplication.Views.Users;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.tuishan.myapplication.Base.BaseContest;
import com.tuishan.myapplication.Custom.LoadingView;
import com.tuishan.myapplication.MyApplication;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.WeakHashMap;

import okhttp3.Request;
import okhttp3.Response;

public class SetEmailActivity extends Activity implements OnTitleBarListener, View.OnClickListener {
    LoadingView Loading;
    TitleBar titleBar;
    Context mContext;
    Button change_Email_btn, change_newCode_send_btn, change_oldCode_send_btn;
    EditText change_email_edit,change_oldCode_edit,change_newCode_edit;


    private static long TOTAL_TIME = 60000;
    private final static long ONECE_TIME = 1000;
    MyApplication myApp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_email);
        mContext = this;
        myApp=(MyApplication)getApplication();
        initView();
    }

    private void initView() {
        Loading = new LoadingView(mContext);
        titleBar = findViewById(R.id.titleBar);
        change_Email_btn = findViewById(R.id.change_Email_btn);
        change_newCode_send_btn = findViewById(R.id.change_newCode_send_btn);
        change_oldCode_send_btn = findViewById(R.id.change_oldCode_send_btn);
        change_email_edit = findViewById(R.id.change_email_edit);
        change_oldCode_edit = findViewById(R.id.change_oldCode_edit);
        change_newCode_edit = findViewById(R.id.change_newCode_edit);
        titleBar.setOnTitleBarListener(this);
        change_Email_btn.setOnClickListener(this);
        change_newCode_send_btn.setOnClickListener(this);
        change_oldCode_send_btn.setOnClickListener(this);
    }

    /**
     * 时间定时器
     */
    private CountDownTimer countDownTimerOld = new CountDownTimer(TOTAL_TIME, ONECE_TIME) {
        @Override
        public void onTick(long millisUntilFinished) {
            String value = String.valueOf((int) (millisUntilFinished / 1000));
            change_oldCode_send_btn.setText(value);
        }

        @Override
        public void onFinish() {
            change_oldCode_send_btn.setText(getResources().getString(R.string.phone_code));
            change_oldCode_send_btn.setEnabled(true);
        }
    };

    /**
     * 时间定时器
     */
    private CountDownTimer countDownTimerNew = new CountDownTimer(TOTAL_TIME, ONECE_TIME) {
        @Override
        public void onTick(long millisUntilFinished) {
            String value = String.valueOf((int) (millisUntilFinished / 1000));
            change_newCode_send_btn.setText(value);
        }

        @Override
        public void onFinish() {
            change_newCode_send_btn.setText(getResources().getString(R.string.phone_code));
            change_newCode_send_btn.setEnabled(true);
        }
    };


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_Email_btn:
                ChangeEmail(change_oldCode_edit.getText().toString().trim(),
                        change_email_edit.getText().toString().trim(),
                        change_newCode_edit.getText().toString().trim());
                break;
            case R.id.change_newCode_send_btn:
                if (!RegexUtils.isEmail(change_email_edit.getText().toString().trim())) {
                    Toast.makeText(this, "请确认邮箱输入是否正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                change_newCode_send_btn.setEnabled(false);
                countDownTimerNew.start();
                SendCodeNew(change_email_edit.getText().toString().trim());
                break;
            case R.id.change_oldCode_send_btn:
                change_oldCode_send_btn.setEnabled(false);
                countDownTimerOld.start();
                SendCodeOld();
                break;
        }
    }

    /**
     * 发送新验证码
     */
    private void ChangeEmail(String oldEmailCode,String newEmail,String newEmailCode) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("oldEmailCode", oldEmailCode);
        objectMap.put("newEmail", newEmail);
        objectMap.put("newEmailCode", newEmailCode);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.ChangeEmailUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object o) {
                Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
                myApp.setLogin(false);
                Loading.dismiss();
                finish();
                SPUtils.getInstance().remove(BaseContest.UserToken);

            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Toast.makeText(mContext, "接口失败", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Toast.makeText(mContext, "修改失败", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }
        });
    }


    /**
     * 发送老验证码
     */
    private void SendCodeOld() {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("typeCode", 6);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.SendEmailToken, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object o) {
                Toast.makeText(mContext, "验证码发送成功", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Toast.makeText(mContext, "接口失败", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Toast.makeText(mContext, "验证码发送失败", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }
        });
    }
    /**
     * 发送新验证码
     */
    private void SendCodeNew(String email) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("email", email);
        objectMap.put("typeCode", 6);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.SendEmail, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object o) {
                Toast.makeText(mContext, "验证码发送成功", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Toast.makeText(mContext, "接口失败", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Toast.makeText(mContext, "验证码发送失败", Toast.LENGTH_SHORT).show();
                Loading.dismiss();
            }
        });
    }

    @Override
    public void onLeftClick(View v) {
        finish();
    }

    @Override
    public void onTitleClick(View v) {

    }

    @Override
    public void onRightClick(View v) {

    }
}
